package com.data.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.config.service.SystemParamsService;
import com.data.entity.BaseData;
import com.data.service.BaseDataDtoService;
import com.data.service.BaseDataService;
import com.data.service.ChargeDayService;
import com.data.service.ChargeMinService;
import com.data.service.ChargeMonthService;
import com.data.service.ChargeShiftService;
import com.data.service.ChargeWeekService;
import com.data.service.StateDayService;
import com.data.service.StateMinService;
import com.data.service.StateMonthService;
import com.data.service.StateShiftService;
import com.data.service.StateWeekService;
/**
 * 按半小时，班次，天，周，月 分层提取设备电费记录和设备状态记录
 * @author Administrator
 */
@Component("dataTransferTask")
public class DataTransferTask {
	@Resource(name="baseDataService")
	private BaseDataService baseDataService;
	@Resource(name="baseDataDtoService")
	private BaseDataDtoService baseDataDtoService;
	@Resource(name="systemParamsService")
	private SystemParamsService systemParamsService;
	@Resource(name="chargeMinService")
	private ChargeMinService chargeMinService;
	@Resource(name="chargeShiftService")
	private ChargeShiftService chargeShiftService;
	@Resource(name="chargeDayService")
	private ChargeDayService chargeDayService;
	@Resource(name="chargeWeekService")
	private ChargeWeekService chargeWeekService;
	@Resource(name="chargeMonthService")
	private ChargeMonthService chargeMonthService;
	
	@Resource(name="stateMinService")
	private StateMinService stateMinService;
	@Resource(name="stateShiftService")
	private StateShiftService stateShiftService;
	@Resource(name="stateDayService")
	private StateDayService stateDayService;
	@Resource(name="stateWeekService")
	private StateWeekService stateWeekService;
	@Resource(name="stateMonthService")
	private StateMonthService stateMonthService;
	
//	private boolean firstTime=true;
	
	public void transferTask(){
		try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
				SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Calendar now=Calendar.getInstance();
				Date currentTime=now.getTime();
				int minute=now.get(Calendar.MINUTE);
				if(minute>=0&&minute<30){
						currentTime=sdft.parse(sdf.format(currentTime)+":00:00");
				}else if(minute>=30){
						currentTime=sdft.parse(sdf.format(currentTime)+":30:00");
				}
				//开始分层提取数据
//				if(firstTime){
//					firstTime=false;
				    Date processTime=systemParamsService.getProcessTime();
				    Calendar processCalendar=Calendar.getInstance();
				    if(processTime!=null){
						processCalendar.setTime(processTime);
						processCalendar.add(Calendar.MINUTE, 30);
				    }else{
				    	BaseData baseData=baseDataService.getFirstBaseData();
				    	if(baseData.getTime()!=null){
							processCalendar.setTime(baseData.getTime());
							int min=processCalendar.get(Calendar.MINUTE);
							if(min>=0&&min<30){
									processCalendar.setTime(sdft.parse(sdf.format(processCalendar.getTime())+":00:00"));
							}else if(min>=30){
									processCalendar.setTime(sdft.parse(sdf.format(processCalendar.getTime())+":30:00"));
							}
				    	}
				    }
					while(processCalendar.before(now) || processCalendar.equals(now)){
						Date belongDate=processCalendar.getTime();
						//半小时电费记录
						chargeMinService.saveChargeMin(belongDate);
						//班次电费记录
					    chargeShiftService.saveChargeShift(belongDate);
					    //每天电费记录
					    chargeDayService.saveChargeDay(belongDate);
					    //每周电费记录
					    chargeWeekService.saveChargeWeek(belongDate);
					    //每月电费记录
					    chargeMonthService.saveChargeMonth(belongDate);
					    
					    //半小时设备状态记录
					    stateMinService.saveStateMin(belongDate);
					    //班次设备状态记录
					    stateShiftService.saveStateShift(belongDate);
					    //每天设备状态记录
					    stateDayService.saveStateDay(belongDate);
					    //每周设备状态记录
					    stateWeekService.saveStateWeek(belongDate);
					    //每月设备状态记录
					    stateMonthService.saveStateMonth(belongDate);
					    processCalendar.add(Calendar.MINUTE, 30);
					}
//				}else{
//				//半小时电费记录
//				chargeMinService.saveChargeMin(currentTime);
//				//班次电费记录
//		    	chargeShiftService.saveChargeShift(currentTime);
//		    	//每天电费记录
//			    chargeDayService.saveChargeDay(currentTime);
//			    //每周电费记录
//			    chargeWeekService.saveChargeWeek(currentTime);
//			   //每月电费记录
//			    chargeMonthService.saveChargeMonth(currentTime);
//			      
//			    //半小时设备状态记录
//			    stateMinService.saveStateMin(currentTime);
//			   //班次设备状态记录
//			   stateShiftService.saveStateShift(currentTime);
//			  //每天设备状态记录
//			   stateDayService.saveStateDay(currentTime);
//			  //每周设备状态记录
//			   stateWeekService.saveStateWeek(currentTime);
//			   //每月设备状态记录
//			   stateMonthService.saveStateMonth(currentTime);
//			  }
			  systemParamsService.setProcessTime(currentTime);
		     }catch (ParseException e) {
			       e.printStackTrace();
	     }
    }
}
